package com.example.chengww.myone.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.chengww.myone.R;
import com.example.chengww.myone.bean.Constants;
import com.example.chengww.myone.bean.read_content.ReadEssay;
import com.example.chengww.myone.bean.read_content.ReadQuestin;
import com.example.chengww.myone.bean.read_content.ReadSerial;
import com.example.chengww.myone.utils.HTTPUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.InputStream;

public class ReadEssayActivity extends AppCompatActivity {

    private TextView tv_essay_title,tv_essay_content,essay_time,essay_author;
    private ImageView img_essay_back,essay_img_author;
    private LinearLayout include1,include2,include3;
    private ImageView imageView2,imageView3;
    private TextView textView1,textView2,textView3;
    private ReadEssay.Data myEssayData;
    private ReadSerial.Data mySerialData;
    private ReadQuestin.Data myQuestionData;
    private int type;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 100){
                //数据已下载好
                String imgPath;
                String content;

                switch (type){
                    case 0:
                        imgPath = myEssayData.getAuthor().get(0).getWeb_url();
                        if (imgPath != null && imgPath.length() != 0){
                            Picasso.with(ReadEssayActivity.this).
                                    load(imgPath).into(essay_img_author);
                        }
                        content = myEssayData.getHp_content();
                        content = content.replace("<br>\r\n","\n\u3000\u3000");
                        content = content.replace("<br>\n","\n\u3000\u3000");
                        content = content.replace("<br>","\n\u3000\u3000");
                        tv_essay_content.setText("\u3000\u3000"+content);
                        tv_essay_title.setText("短篇");
                        textView1.setText(String.valueOf(myEssayData.getPraisenum()));
                        textView2.setText(String.valueOf(myEssayData.getCommentnum()));
                        textView3.setText(String.valueOf(myEssayData.getSharenum()));

                        essay_author.setText(myEssayData.getHp_author());
                        essay_time.setText(myEssayData.getAuth_it());
                        break;
                    case 1:
                        imgPath = mySerialData.getAuthor().getWeb_url();
                        Picasso.with(ReadEssayActivity.this).load(imgPath).into(essay_img_author);
                        content = mySerialData.getContent();
                        content = content.replace("<br>\r\n","\n\u3000\u3000");
                        content = content.replace("<br>\n","\n\u3000\u3000");
                        content = content.replace("<br>","\n\u3000\u3000");
                        tv_essay_content.setText("\u3000\u3000"+content);
                        tv_essay_title.setText("连载");
                        textView1.setText(String.valueOf(mySerialData.getPraisenum()));
                        textView2.setText(String.valueOf(mySerialData.getCommentnum()));
                        textView3.setText(String.valueOf(mySerialData.getSharenum()));

                        essay_author.setText(mySerialData.getAuthor().getUser_name());
                        essay_time.setText(mySerialData.getExcerpt());
                        break;
                    case 2:

                        content = myQuestionData.getQuestion_title()+"<br>"+
                                myQuestionData.getQuestion_content()+"<br>"+
                                myQuestionData.getAnswer_title()+"<br>"+
                                myQuestionData.getAnswer_content();
                        content = content.replace("<br>\r\n","\n\u3000\u3000");
                        content = content.replace("<br>\n","\n\u3000\u3000");
                        content = content.replace("<br>","\n\u3000\u3000");
                        tv_essay_content.setText("\u3000\u3000"+content);
                        tv_essay_title.setText("问答");
                        textView1.setText(String.valueOf(myQuestionData.getPraisenum()));
                        textView2.setText(String.valueOf(myQuestionData.getCommentnum()));
                        textView3.setText(String.valueOf(myQuestionData.getSharenum()));

                        essay_time.setText(myQuestionData.getQuestion_title());
                        essay_time.setTextSize(20);
                        break;
                }

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_essay);

        tv_essay_title = (TextView) findViewById(R.id.tv_essay_title);
        tv_essay_content = (TextView) findViewById(R.id.tv_essay_content);
        img_essay_back = (ImageView) findViewById(R.id.img_essay_back);
        essay_img_author = (ImageView) findViewById(R.id.essay_img_author);

        essay_time = (TextView) findViewById(R.id.essay_tv_time);
        essay_author = (TextView) findViewById(R.id.essay_tv_author);

        include1 = (LinearLayout) findViewById(R.id.include_1);
        include2 = (LinearLayout) findViewById(R.id.include_2);
        include3 = (LinearLayout) findViewById(R.id.include_3);

        textView1 = (TextView) include1.getChildAt(1);
        imageView2 = (ImageView) include2.getChildAt(0);
        imageView2.setImageResource(R.mipmap.comment_image);
        textView2 = (TextView) include2.getChildAt(1);
        imageView3 = (ImageView) include3.getChildAt(0);
        imageView3.setImageResource(R.mipmap.share_image);
        textView3 = (TextView) include3.getChildAt(1);

        img_essay_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.activity_in,R.anim.activity_out);
            }
        });
        Intent intent = getIntent();
        type = intent.getIntExtra("type",0);
        final String essayId = intent.getStringExtra("id");
        final boolean hasRadio = intent.getBooleanExtra("radio",false);
        //加载网络数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream in = null;
                switch (type){
                    case 0:
                        in = HTTPUtils.getInputStream(
                                Constants.READING.ESSAY_CONTENT+essayId);
                        String json = HTTPUtils.getStringByInputStream(in);
                        Gson gson = new Gson();
                        ReadEssay readEssay = gson.fromJson(json,ReadEssay.class);
                        myEssayData = readEssay.getData();
                        break;
                    case 1:
                        in = HTTPUtils.getInputStream(
                                Constants.READING.SERIAL_CONTENT+essayId);
                        String json1 = HTTPUtils.getStringByInputStream(in);
                        Gson gson1 = new Gson();
                        ReadSerial readSerial= gson1.fromJson(json1,ReadSerial.class);
                        mySerialData = readSerial.getData();
                        break;
                    case 2:
                        in = HTTPUtils.getInputStream(
                                Constants.READING.QUESTION_CONTENT+essayId);
                        String json2 = HTTPUtils.getStringByInputStream(in);
                        Gson gson2 = new Gson();
                        ReadQuestin readQuestin= gson2.fromJson(json2,ReadQuestin.class);
                        myQuestionData = readQuestin.getData();
                        break;
                }


                handler.obtainMessage(100).sendToTarget();
            }
        }).start();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_in,R.anim.activity_out);
    }


}
